
import {Injectable} from "@angular/core";
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import {AuthService} from "../service/AuthService";

@Injectable({
  providedIn:'root'
})
export class AuthGuard implements CanActivate{
  constructor(private authService: AuthService, private router: Router) { }
  // route: ActivatedRouteSnapshot,  如果有错的话，试着改下这个地方
  token: string | undefined;
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    let userInfo = this.authService.isUserLoggedIn();
    if (userInfo) {
      return true;
    } else {
      console.log('用户未登录，跳转到登陆页面');
      this.router.navigate(['/user/login']);
      return false;
    }
  }

}
